package dc.connectionmanager.logic;

import java.awt.EventQueue;

public class AsyncCommandHandler extends Thread
{
	IConnectionManager cm;
	IArgument arg_cmd = null;
	String str_cmd = null;
	
	public AsyncCommandHandler(IConnectionManager cmHandle)
	{
		this.cm = cmHandle;
	}
	
	public AsyncCommandHandler(IConnectionManager cmHandle, boolean noCallback)
	{
		this.cm = cmHandle;
	}
	
	public void runCommand(IArgument arg)
	{
		this.arg_cmd = arg;
		this.start();
	}
	
	public void runCommand(String command)
	{
		this.str_cmd = command;
		this.start();
	}
	
    @Override
	public void run()
	{
	    if(arg_cmd != null)
	    {
	        final Object result = (Object)this.cm.sendCommand(((AArgument)arg_cmd).arg_2);
            EventQueue.invokeLater( new Runnable()
            {
                @Override
                public void run() 
                {
                    ((AArgument)arg_cmd).arg_1.handleResult(result);
                }
            });
	    }
	    else
	    {
	        this.cm.sendCommandNC(str_cmd);
	    }
	}
}